package com.campusflea.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.campusflea.DateBase.ShopInfo;
import com.campusflea.MainActivity;
import com.campusflea.R;
import com.campusflea.ReleaseActivity;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.DownloadFileListener;
import cn.bmob.v3.listener.QueryListener;

/**
 * Created by Administrator on 2017/6/1 0001.
 */

public class FragmentRelease extends Fragment{
    private Button btn1;
    private List<ShopInfo> list;
    private ImageView imageView;
    private String phone;
    public FragmentRelease(String phone){
        this.phone=phone;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.release_layout, null); // release_layout.xml布局，得到一个view
        btn1=(Button)rootView.findViewById(R.id.button1);
        //imageView=(ImageView)rootView.findViewById(R.id.imageView3) ;
        btn1.setOnClickListener(lisenter);
        // TODO Auto-generated method stub
        //return inflater.inflate(R.layout.release_layout, container, false);
        BmobQuery<ShopInfo> query = new BmobQuery<ShopInfo>();
        query.getObject("eda84bdfb4", new QueryListener<ShopInfo>() {

            @Override
            public void done(ShopInfo object, BmobException e) {
                if(e==null){
                    //object.getBmobFile();
                    downloadImage(object);
                }else{
                    //Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                }
            }

        });
//        ArrayList<HashMap<String, Object>> listArray = new ArrayList<HashMap<String, Object>>();
//        String bql ="select * from ShopInfo";
//
//        BmobQuery<ShopInfo> query=new BmobQuery<ShopInfo>();
//        query.setSQL(bql);
//        query.doSQLQuery(new SQLQueryListener<ShopInfo>(){
//
//            @Override
//            public void done(BmobQueryResult<ShopInfo> bmobQueryResult, BmobException e) {
//                list = (List<ShopInfo>) bmobQueryResult.getResults();
//                if(list.size()==0){
//                    Toast.makeText(getActivity(),"获取信息失败", Toast.LENGTH_LONG).show();
//                }
//                else {
//
//                    for(int i=0;i<1;i++){
//                        //Bitmap bitmap= getHttpBitmap(list.get(0).getBmobFile().getFileUrl());
//                        //Toast.makeText(getActivity(),list.get(0).getBmobFile().getFileUrl(),Toast.LENGTH_LONG).show();
//                        //imageView.setImageBitmap(bitmap);
//                    }
//                }
//            }
//        });
        return rootView;
    }
    //下载图片
    private void downloadImage(ShopInfo shopInfo) {
        BmobFile icon=shopInfo.getBmobFile();
        icon.download(new DownloadFileListener() {

            @Override
            public void onProgress(Integer integer, long l) {

            }

            @Override
            public void done(String s, BmobException e) {
                imageView.setImageBitmap(BitmapFactory.decodeFile(s));   //根据地址解码并显示图片
            }
        });
    }
    public Bitmap getHttpBitmap(String url) {
        Bitmap bitmap=null;
        URL myUrl;

        try {
            myUrl=new URL(url);
            HttpURLConnection conn=(HttpURLConnection)myUrl.openConnection();
            conn.setConnectTimeout(5000);
            conn.connect();
            InputStream is=conn.getInputStream();
            bitmap=BitmapFactory.decodeStream(is);
            //把bitmap转成圆形
            //bitmap=toRoundBitmap(bitmap);
            is.close();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //返回圆形bitmap
        return bitmap;
    }
    private View.OnClickListener lisenter = new View.OnClickListener() {
        public void onClick(View v) {
            Button btn =(Button)v;
            try{
                switch(btn.getId())
                {
                    case R.id.button1:
                    {
                        Intent intent0 = new Intent(getActivity(),ReleaseActivity.class);
                        //ReleaseActivity releaseActivity = new ReleaseActivity(phone);
                        intent0.putExtra("phone",phone);
                        startActivity(intent0);
                        break;
                    }
                }
            }catch(Exception e){}
        }
    };
}
